package com.sghd.football.module.game.service.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.sghd.football.module.game.service.core.ChessType;
import com.sghd.football.utils.json.Json;

import java.io.IOException;
import java.util.*;

/**
 * \* Created with 王创辉.
 * \* Date: 2018/5/18
 * \* Time: 15:25
 * \* @desc:
 */
@Json
public class Result {

    @JsonIgnore
    private int totalLine;

    //@JsonSerialize(using = KeySerialize.class)
    private Map<Integer, Integer[]> gameResult = new HashMap<>();

    private boolean bounds = false;
    /**
     * 是否触发免费旋转
     */
    @JsonIgnore
    private boolean freeGame = false;
    /**
     * 本局得分
     */
    private long totalFen;


    /**
     * 本局下注总金额(线*倍率)
     */
    @JsonIgnore
    private int totalBet;
    /**
     * 当前结果是不是免费旋转的
     */
    private boolean currentFree;
    /**
     * 当前排列
     */
    private List<Integer> sequence = new ArrayList<>();
    /** 区分是普通旋转的游戏结果还是小游戏的游戏结果 */
    @JsonIgnore
    private boolean mgame = false;

    public Result() {
    }

    public static Result valueOf(boolean mgame, long totalFen, List<Integer> sequence) {
        Result result = new Result();
        result.mgame = mgame;
        result.totalFen = totalFen;
        result.sequence = sequence;
        return result;
    }

    public void addLine(int lineIdx, Integer[] rset) {
        lineIdx = lineIdx + 1;
        gameResult.put(lineIdx, rset);
        totalLine++;
    }

    public int getTotalLine() {
        return totalLine;
    }

    public void setTotalLine(int totalLine) {
        this.totalLine = totalLine;
    }

    public Map<Integer, Integer[]> getGameResult() {
        return gameResult;
    }

    public void setGameResult(Map<Integer, Integer[]> gameResult) {
        this.gameResult = gameResult;
    }


    public void print() {
        for (Integer linIdx : gameResult.keySet()) {
            System.out.println("LINE:" + linIdx);
            System.out.println(Arrays.toString(gameResult.get(linIdx)));
        }
        System.out.println("中[" + totalLine + "]条线");
        System.out.println("进入小游戏：" + bounds);
        System.out.println("中免费免费游戏：" + freeGame);
    }

    /**
     * 计算得分
     */
    public Result calc(int base) {
        for (Integer[] idxs : gameResult.values()) {
            //中奖元素类型(索引)
            Integer retIdx = Arrays.stream(idxs).filter(idx -> {
                int chessIdx = sequence.get(idx);
                return ChessType.values()[chessIdx] != ChessType.CT_WILD;
            }).findFirst().get();
            ChessType chessType = ChessType.values()[sequence.get(retIdx)];
            int bet = chessType.getRate(idxs.length);//元素个数对应的倍率
            totalFen += base * bet;
        }
        return this;
    }

    /**
     * 只序列化keySet
     */
    public static class KeySerialize extends JsonSerializer<Map<Integer, Integer[]>> {
        @Override
        public void serialize(Map<Integer, Integer[]> params, JsonGenerator jgen,
                              SerializerProvider provider)
                throws IOException, JsonProcessingException {
            if(params != null){
                jgen.writeObject(params.keySet());
            }else {
                jgen.writeNull();
            }
        }
    }

    public void setBounds(boolean bounds) {
        this.bounds = bounds;
    }

    public void setFreeGame(boolean freeGame) {
        this.freeGame = freeGame;
    }

    public boolean isBounds() {
        return bounds;
    }

    public boolean isFreeGame() {
        return freeGame;
    }

    public long getTotalFen() {
        return totalFen;
    }

    public void setTotalFen(long totalFen) {
        this.totalFen = totalFen;
    }

    public int getTotalBet() {
        return totalBet;
    }

    public void setTotalBet(int totalBet) {
        this.totalBet = totalBet;
    }

    public boolean isCurrentFree() {
        return currentFree;
    }

    public Result setCurrentFree(boolean currentFree) {
        this.currentFree = currentFree;
        return this;
    }

    public List<Integer> getSequence() {
        return sequence;
    }

    public void setSequence(List<Integer> sequence) {
        this.sequence = sequence;
    }

    /** 当前结果是否是小游戏*/
    public boolean isMgame() {
        return mgame;
    }

    public void setMgame(boolean mgame) {
        this.mgame = mgame;
    }

}
